package NetWork.Ydlchat.util;

import NetWork.Ydlchat.Message;

import java.io.*;
import java.util.Optional;

public class MsgUtils {
    public static Optional<Message> redMsg(InputStream inputStream) {
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(inputStream);
            return Optional.ofNullable((Message) ois.readObject());
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        return Optional.empty();
    }

    public static void writeMsg(OutputStream outputStream, Message message) {
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(outputStream);
            oos.writeObject(message);
            oos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
